package com.example.demo.factory;

import com.example.demo.Util.UtilBean;
import com.example.demo.entity.Module;
import com.example.demo.entity.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class StudentManageFactory {

    public static final String YYYY = "yyyy";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public Student createStudent(String studentType,String studentName,String birthDay,Integer age,String supervisorName){

        Student student;

        if ("UG".equals(studentType) && age >= 17) {
            student = new UG();
        } else if ("PGT".equals(studentType) && age >= 20) {
            student = new PGT();
        } else if ("PGR".equals(studentType) && age >= 20) {
            student = new PGR();
        }else {
            System.out.println("学生年龄太小，不能注册");
            return null;
        }
        student.setAge(age);

        //setStudentName
        String[] s = studentName.split(" ");
        if (null== s || s.length != 2) {
            System.out.println("wrong parameter!");
            return null;
        }
        student.setName(new StudentName(s[0],s[1]));

        //set BirthDay and age
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            student.setBirthDay(sdf.parse(birthDay));
        } catch (ParseException e) {
            System.out.println("wrong parameter!");
            return null;
        }
        student.setAge(age);

        return student;

    }

    private StudentId getStudentId(){
        String first = String.valueOf(UtilBean.getInstance().getStudentLabelSeq());
        Integer last = UtilBean.getInstance().getStudentSeq();
        if (last == 9999) {
            first += 1;
        }
        return new StudentId(String.valueOf(first),last);
    }

    public Integer noOfStudents(String typeOfStudent){
        Map<String, Student> studentMap = UtilBean.getInstance().getStudentMap();
        Integer count = 0;
        for (Student value : studentMap.values()) {
            if (value.getStudentType().equals(typeOfStudent)) {
                count++;
            }
        }
        return count;
    }

    public Student registerStudent(Student student){
        List<String> supervisorName = UtilBean.getInstance().getSupervisorName();

        try {
            student.setStudentId(getStudentId());
            if (student.getStudentType().equals("PGR")) {
                // 随机指定随机指定一个导师
                // 如果要取一个范围在[min,max]之间的随机整数那么公式如下
                // r.nextInt(max - min + 1) + min;
                Random r = new Random();
                int randNumber =r.nextInt(supervisorName.size() + 1);
                student.setSupervisorName(supervisorName.get(randNumber));
            }
            student.setModules(getStudentModules(student.getStudentType()));
            student.setSmartCard(getStudentSmartCard(student));
            UtilBean.getInstance().addStudentMap(student.getStudentId().toString(),student);
        }catch (Exception e){
            return null;
        }
        return student;
    }

    private List<Module> getStudentModules(String studentType){
        List<Module> allModule = UtilBean.getInstance().getModules();
        List<Module> studentModule = new ArrayList<>();
        Integer credits = 0;
        if ("UG".equals(studentType)) {
            for (Module module : allModule) {
                if (credits < 120) {
                    credits += Integer.parseInt(module.getCredits());
                    studentModule.add(module);
                }
            }
        } else if ("PGT".equals(studentType)) {
            for (Module module : allModule) {
                if (credits < 180) {
                    credits += Integer.parseInt(module.getCredits());
                    studentModule.add(module);
                }
            }
        }else {
            return null;
        }
        return studentModule;
    }

    public void amendStudentData(String studentId,Student student){
        Map<String, Student> studentMap = UtilBean.getInstance().getStudentMap();
        studentMap.put(studentId,student);
        System.out.println("修改成功");
    }

    public void terminateStudent (String studentId){
        Map<String, Student> studentMap = UtilBean.getInstance().getStudentMap();
        studentMap.remove(studentId);
        System.out.println("删除成功");
    }

    private SmartCard getStudentSmartCard(Student student){
        if ("UG".equals(student.getStudentType())) {
            if (student.getAge() < 17) {
                System.out.println("发行智能卡失败，学生年龄不足17岁");
            }
        }else {
            if (student.getAge() < 20) {
                System.out.println("发行智能卡失败，学生年龄不足20岁");
            }
        }

        Date issueDate = new Date();
        SmartCard smartCard = new SmartCard();
        smartCard.setStudentName(student.getName());
        smartCard.setStudentBirthday(student.getBirthDay());
        smartCard.setIssueDate(issueDate,student.getStudentType());
        smartCard.setSmartCardId(getStuSmartCardId(student.getName()));

        return smartCard;
    }

    private SmartCardId getStuSmartCardId(StudentName studentName){
        String first = studentName.getFirstName().substring(0, 1);
        String last = studentName.getLastName().substring(0,1);
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY);

        String name = first+last;
        Integer year = Integer.parseInt(sdf.format(new Date()));
        Integer cardSeq = UtilBean.getInstance().getCardSeq();

        return new SmartCardId(name, year, cardSeq);


    }

}
